﻿// CComDlg.cpp: 实现文件
//

#include "pch.h"
#include "SP.h"
#include "CComDlg.h"
#include "afxdialogex.h"
#include "CConfig.h"

// CComDlg 对话框

IMPLEMENT_DYNAMIC(CComDlg, CDialogEx)

CComDlg::CComDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DLG_COM, pParent)
	, m_ComNum(_T(""))
	, m_ComBaudrate(_T(""))
	, PassPCBSNFlag(FALSE)
	, SNLengthCheck(FALSE)
	, PCBSNLength(0)
	, QiankeSNLength(0)
	, PassQKSNFlag(FALSE)
	, dianpiancheck(FALSE)
	, m_buchang(0)
	, PassFixFlag(FALSE)
{

}

CComDlg::~CComDlg()
{
}

void CComDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT_COMNUM, m_ComNum);
	DDX_Text(pDX, IDC_EDIT_COMPORT, m_ComBaudrate);
	DDX_Check(pDX, IDC_CHECK2, PassPCBSNFlag);
	DDX_Check(pDX, IDC_CHECK1, SNLengthCheck);
	DDX_Text(pDX, IDC_EDIT1, PCBSNLength);
	DDX_Text(pDX, IDC_EDIT2, QiankeSNLength);
	DDX_Check(pDX, IDC_CHECK3, PassQKSNFlag);
	DDX_Check(pDX, IDC_CHECK4, dianpiancheck);
	DDX_Text(pDX, IDC_EDIT3, m_buchang);
	DDX_Check(pDX, IDC_CHECK5, PassFixFlag);
}

BOOL CComDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();
	CString str;
	str.Format(L"%s长度", Manager.TiaomaName1);
	SetDlgItemTextW(IDC_STATIC11, str);
	str.Format(L"%s长度", Manager.TiaomaName2);
	SetDlgItemTextW(IDC_STATIC12, str);
	str.Format(L"屏蔽%s", Manager.TiaomaName1); 
	SetDlgItemTextW(IDC_CHECK2, str);
	str.Format(L"屏蔽%s", Manager.TiaomaName2);
	SetDlgItemTextW(IDC_CHECK3, str);
	ReadConfig();
	return TRUE;
}



void CComDlg::ReadConfig()
{
	m_ComNum.Format(L"%d", Manager.m_nScrewCom);
	m_ComBaudrate.Format(L"%d",Manager.m_nScrewPort);
	SNLengthCheck = Manager.SNLengthCheck;
	PCBSNLength = Manager.PCBSNLength;
	QiankeSNLength = Manager.QiankeSNLength;
	PassPCBSNFlag = Manager.PassPCBSNFlag;
	PassQKSNFlag = Manager.PassQKSNFlag;
	dianpiancheck = Manager.dianpiancheck;
	m_buchang = Manager.buchang;
	PassFixFlag = Manager.PassFixFlag;
	UpdateData(FALSE);
}

void CComDlg::SaveConfig()
{
	UpdateData(TRUE);
	CString str;
	Manager.m_RS232Config.SaveConfigIni(L"RS232", L"m_nScrewCom", m_ComNum);
	Manager.m_RS232Config.SaveConfigIni(L"RS232", L"m_nScrewPort", m_ComBaudrate);
	str.Format(L"%d", m_nScrewFlag);
	Manager.m_RS232Config.SaveConfigIni(L"RS232", L"m_nScrewFlag", str);

	Manager.SNLengthCheck = SNLengthCheck;
	Manager.PCBSNLength = PCBSNLength;
	Manager.QiankeSNLength = QiankeSNLength;

	
	Manager.PassFixFlag = PassFixFlag;
	Manager.PassPCBSNFlag = PassPCBSNFlag;
	Manager.PassQKSNFlag = PassQKSNFlag;
	Manager.dianpiancheck = dianpiancheck;
	Manager.buchang = m_buchang;
	CString cstr;
	cstr.Format(L"%d", Manager.PassFixFlag);
	Manager.m_RS232Config.SaveConfigIni(L"RS232", L"PassFixFlag", cstr);

	cstr.Format(L"%d", Manager.PassPCBSNFlag);
	Manager.m_RS232Config.SaveConfigIni(L"RS232", L"PassPCBSNFlag", cstr);
	cstr.Format(L"%d", Manager.PassQKSNFlag);
	Manager.m_RS232Config.SaveConfigIni(L"RS232", L"PassQKSNFlag", cstr);
	cstr.Format(L"%d", Manager.SNLengthCheck);
	Manager.m_RS232Config.SaveConfigIni(L"RS232", L"SNLengthCheck", cstr);
	cstr.Format(L"%d", Manager.PCBSNLength);
	Manager.m_RS232Config.SaveConfigIni(L"RS232", L"PCBSNLength", cstr);
	cstr.Format(L"%d", Manager.QiankeSNLength);
	Manager.m_RS232Config.SaveConfigIni(L"RS232", L"QiankeSNLength", cstr);
	cstr.Format(L"%d", Manager.dianpiancheck);
	Manager.m_RS232Config.SaveConfigIni(L"RS232", L"dianpian", cstr);
	

	cstr.Format(L"%d", Manager.buchang);
	Manager.m_RS232Config.SaveConfigIni(L"RS232", L"buchang", cstr);

	Manager.GetLog()->DisplayAndSaveLog(L"RS232保存数据完成", LV_EVENT);

	Manager.ReadComConfig();
}


BEGIN_MESSAGE_MAP(CComDlg, CDialogEx)
	ON_BN_CLICKED(IDC_BTN_SAVE, &CComDlg::OnBnClickedBtnSave)
	ON_BN_CLICKED(IDC_BTN_START, &CComDlg::OnBnClickedBtnStart)
	ON_BN_CLICKED(IDC_BUTTON2, &CComDlg::OnBnClickedButton2)
	ON_BN_CLICKED(IDC_BUTTON3, &CComDlg::OnBnClickedButton3)
END_MESSAGE_MAP()


// CComDlg 消息处理程序


void CComDlg::OnBnClickedBtnSave()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	Manager.m_nScrewCom = _ttoi(m_ComNum);
	Manager.m_nScrewPort = _ttoi(m_ComBaudrate);
	SaveConfig();
	//ReadConfig();
}


void CComDlg::OnBnClickedBtnStart()
{
	// TODO: 在此添加控件通知处理程序代码

	if (Manager.m_cComScrew.get_com_port() == NULL) {
		int err = Manager.m_cComScrew.com_open(Manager.m_nScrewCom, Manager.m_nScrewPort, 0);
		if (err != 1) {
			CString cstr;
			cstr.Format(L"Com1 Error %d", err);
			Manager.GetLog()->DisplayAndSaveLog(cstr.GetBuffer(), LV_EVENT);
			Manager.ComScrewConnectFlag = 0;
		}
		else
		{
			Manager.ComScrewConnectFlag = 1;
			CString cstr;
			cstr.Format(L"com open success");
			Manager.GetLog()->DisplayAndSaveLog(cstr.GetBuffer(), LV_EVENT);
		}
	}
}



void CComDlg::OnBnClickedButton2()
{
	// TODO: 在此添加控件通知处理程序代码
	Manager.ScrewStartFlag = 1;
}


void CComDlg::OnBnClickedButton3()
{
	// TODO: 在此添加控件通知处理程序代码
	Manager.ScrewResetFlag = 1;
}
